package com.chess;

public class ChessBoard {
	
	private int[][] board;
	
	public ChessBoard(int x_grid_number, int y_grid_number) {
		board = new int[x_grid_number][];
		for (int i = 0; i < x_grid_number; i++) {
			board[i] = new int[y_grid_number];
			for (int j = 0; j < y_grid_number; j++) {
				board[i][j] = 0;
			}
		}
	}
	
	public boolean addPieceByMe(int x, int y) {
		if(board[x][y] == 0) {
			board[x][y] = 1;
			return true;
		}
		return false;
	}
	
	public boolean addPieceByOpponents(int x, int y) {
		if(board[x][y] == 0) {
			board[x][y] = 2;
			return true;
		}
		return false;
	}
	
	public boolean occupiedByMe(int x, int y) {
		return board[x][y] == 1;
	}
	
	public boolean occupiedByOpponents(int x, int y) {
		return board[x][y] == 2;
	}
	
	public boolean win(int newX, int newY) {
		return winInHorizontalDirection(newX, newY) ||
		winInVerticalDirection(newX, newY) ||
		winInLeftTiltDirection(newX, newY) ||
		winInRightTiltDirection(newX, newY);
		
	}
	
	private boolean winInHorizontalDirection(int newX, int newY) {
		int horizontalStart = (newX - 4 < 0) ? 0 : newX - 4;
		int horizontalEnd = (newX + 4 > board.length - 1) ? board.length - 1 : newX + 4;
		
		int countOfContinuousPieces = 0;
		for (int i = horizontalStart; i <= horizontalEnd; i++) {
			if(board[i][newY] == 1) {
				countOfContinuousPieces++;
				if(countOfContinuousPieces >= 5) {
					return true;
				}
			}
			else {
				countOfContinuousPieces = 0;
			}
		}
		return false;
	}
	
	private boolean winInVerticalDirection(int newX, int newY) {
		int verticalStart = (newY - 4 < 0) ? 0 : newY - 4;
		int verticalEnd = (newY + 4 > board[0].length) ? 0 : newY + 4;
		
		int countOfContinuousPieces = 0;
		for (int i = verticalStart; i <= verticalEnd; i++) {
			if(board[newX][i] == 1) {
				countOfContinuousPieces++;
				if(countOfContinuousPieces >= 5) {
					return true;
				}
			}
			else {
				countOfContinuousPieces = 0;
			}
		}
		return false;
	}
	
	private boolean winInLeftTiltDirection(int newX, int newY) {
		int startX = newX;
		int startY = newY;
		while(startX > 0 && startY > 0 && newX - startX < 4) {
			startX--;
			startY--;
		}
		int endX = newX;
		int endY = newY;
		while(endX < board.length - 1 && endY < board[0].length - 1 && endX - newX < 4) {
			endX++;
			endY++;
		}
		int countOfContinuousPieces = 0;
		for (int i = startX, j = startY; i <= endX; i++, j++) {
			if(board[i][j] == 1) {
				countOfContinuousPieces++;
				if(countOfContinuousPieces >= 5) {
					return true;
				}
			}
			else {
				countOfContinuousPieces = 0;
			}
		}
		return false;
	}
	
	private boolean winInRightTiltDirection(int newX, int newY) {
		int startX = newX;
		int startY = newY;
		while(startX > 0 && startY < board[0].length - 1 && newX - startX < 4) {
			startX--;
			startY++;
		}
		int endX = newX;
		int endY = newY;
		while(endX < board.length - 1 && endY > 0 && endX - newX < 4) {
			endX++;
			endY--;
		}
		int countOfContinuousPieces = 0;
		for (int i = startX, j = startY; i <= endX; i++, j--) {
			if(board[i][j] == 1) {
				countOfContinuousPieces++;
				if(countOfContinuousPieces >= 5) {
					return true;
				}
			}
			else {
				countOfContinuousPieces = 0;
			}
		}
		return false;
	}
	
}
